﻿using UnityEngine;
using System.Collections.Generic;
namespace Room
{
    public class RCAIActor : RCAIBase
    {
        public RCAIActor(RoomCharactor owner) 
            :base(owner)
        {
            InitFSM();
        }

        public float height;
        public float hpOffest;
        public float stateTimer = 0;
        public float stateForceBackNormal = 15;


        #region FSM
        public int curState;
        protected Dictionary<int, PlayerAIStateBase> allStates = new Dictionary<int, PlayerAIStateBase>();

        public override void Tick()
        {
            base.Tick();
            allStates[curState].Tick();

            ForceSwitchNormal();
        }

        public PlayerAIStateBase GetCurrState()
        {
            return allStates[curState];
        }

        protected virtual void InitFSM()
        {
            curState = PlayerAIState.Normal;
            
            allStates[PlayerAIState.Normal] = new PlayerAIStateNormal();
            allStates[PlayerAIState.Fishing] = new PlayerAIStateFishing();
            allStates[PlayerAIState.Mining] = new PlayerAIStateMining();
            allStates[PlayerAIState.Shopping] = new PlayerAIStateShopping();
            allStates[PlayerAIState.Death] = new PlayerAIStateDeath();
            allStates[PlayerAIState.Defend] = new PlayerAIStateDefend();
            allStates[PlayerAIState.Attack] = new PlayerAIStateAttack();
            allStates[PlayerAIState.Arress] = new PlayerAIStateArress();

            foreach (var state in allStates)
            {
                state.Value.controller = this;
            }
        }

        public override void SwitchState(int newState)
        {
            if (curState != newState)
            {
                if (allStates.ContainsKey(newState))
                {
                    allStates[curState].Leave();
                    curState = newState;
                    allStates[newState].Enter();
                }
            }
            stateTimer = 0f;
        }
        #endregion
        
        public override void SetHp(float hp)
        {  
        }

        protected void UnloadHp()
        {  
        }

        public GameObject fishObj = null;
        public GameObject mineObj = null;

        public override void DoStop()
        {
            base.DoStop();
            SwitchState(PlayerAIState.None);
            UnloadHp();   
        }

        void ForceSwitchNormal()
        {
            stateTimer += Time.deltaTime;
            if (stateTimer > stateForceBackNormal)
            {
                if (   curState == PlayerAIState.Mining 
                    || curState == PlayerAIState.Fishing 
                    || curState == PlayerAIState.Defend
                    || curState == PlayerAIState.Arress
                    || curState == PlayerAIState.Death
                    )
                {
                    SwitchState(PlayerAIState.Normal);
                }

                stateTimer = 0f;
            }
        }

    }
}
